package com.packt.webstore.domain.repository.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;

@Repository
public class InMemoryProductRepository implements ProductRepository {
	
	private List<Product> listOfProducts = new ArrayList<Product>();
	
	public InMemoryProductRepository(){
		Product iphone = new Product("P1234", "iPhone 5s", new BigDecimal(500));
		iphone.setDescricao("Apple iPhone 5s smartphone with 4.00-inch 640x1136 "
				+ "display and 8-megapixel rear camera");
		iphone.setCategorio("Smart Phone");
		iphone.setFabrica("Apple");
		iphone.setUnidadeDeStock(1000);
		
		Product laptop_dell = new Product("P1235", "Dell Inspiron", new BigDecimal(700));
		laptop_dell.setDescricao("Dell inspiron 14-inch Laptop (Black) with 3rd"
				+ "Generation Intel Core processors");
		laptop_dell.setCategorio("Laptop");
		laptop_dell.setFabrica("Dell");
		laptop_dell.setUnidadeDeStock((long) 1000);
		
		Product tablet_Nexus = new Product ("P1236", "Nexus 7", new BigDecimal(300));
		tablet_Nexus.setDescricao("Google Nexus 7 is the lightest 7 inch tablet with a quad-core Qualcom Snapdragon S4 Pro processor");
		tablet_Nexus.setCategorio("Tablet");
		tablet_Nexus.setFabrica("Google");
		tablet_Nexus.setUnidadeDeStock(1000);
		
		listOfProducts.add(iphone);
		listOfProducts.add(laptop_dell);
		listOfProducts.add(tablet_Nexus);
	}
	
		public List<Product> getAllProducts(){
			
			return listOfProducts;
		}
		
		public Product getProductById(String productId){
			Product productById = null;
			
			for(Product product : listOfProducts){
				if(product != null && product.getProductId() != null &&
						product.getProductId().equals(productId)){
					 productById = product;
					break;
				}
			}
			
			if(productById == null){
				throw new IllegalArgumentException("No products found with the product id: " + productId);
			}
			
			return productById;
		}
		
		public List<Product> getProductsByCategory(String category){
			
			List<Product> productsByCategory = new ArrayList<Product>();
			
			for(Product product: listOfProducts){
				if(category.equalsIgnoreCase(product.getCategorio())){
					
					productsByCategory.add(product);
				}
			}
			
			return productsByCategory;
		}
		
		public Set<Product> getProductsByFilter(Map<String, List<String>> filterParams) {
			Set<Product> productsByBrand = new HashSet<Product>();
			Set<Product> productsByCategory = new HashSet<Product>();
			
			Set<String> criterias = filterParams.keySet();
			
			if (criterias.contains("brand")) {
				for (String brandName: filterParams.get("brand")) {
					for (Product product : listOfProducts) {
						if(brandName.equalsIgnoreCase(product.getFabrica())) {
							productsByBrand.add(product);
						}
					}
				}
			}
			
			if (criterias.contains("category")) {
				for (String category: filterParams.get("category")) {
			productsByCategory.addAll(this.getProductsByCategory(category));
				}
			}
			
			productsByCategory.retainAll(productsByBrand);
			
			return productsByCategory;
		}
		
		public void addProduct(Product product) {
			listOfProducts.add(product);
		}

}
